package com.cloud.flowable.controller;

import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.enums.BusinessType;
import com.cloud.flowable.entity.WfForm;
import com.cloud.flowable.service.IWfFormService;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.utils.ToolUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "流程表单配置")
@RestController
@RequestMapping("/flowable/Form")
public class WfFormController extends BaseController {

    @Autowired
    private IWfFormService formService;

    @Operation(summary = "表单配置分页列表")
    @RequestPermissions("flowable:form:list")
    @GetMapping("/list")
    public Result<WfForm> list(WfForm wfForm) {
        startPage();
        List<WfForm> list = formService.getFormList(wfForm);
        return getDataTable(list);
    }

    @Operation(summary = "表单配置列表")
    @GetMapping("/formList")
    public Result formList(WfForm wfForm) {
        return Result.success("查询成功", formService.getFormList(wfForm));
    }

    @Operation(summary = "表单配置详情")
    @RequestPermissions("flowable:form:query")
    @GetMapping(value = "/getInfo")
    public Result getInfo(Long formId) {
        return Result.success("查询成功", formService.getInfo(formId));
    }

    @Operation(summary = "表单配置新增")
    @RequestPermissions("flowable:form:add")
    @Logs(title = "表单配置新增", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public Result insert(@RequestBody WfForm wfForm) {
        return toResult(formService.saveWfForm(wfForm));
    }

    @Operation(summary = "表单配置修改")
    @RequestPermissions("flowable:form:edit")
    @Logs(title = "表单配置修改", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public Result update(@RequestBody WfForm wfForm) {
        return toResult(formService.updateForm(wfForm));
    }

    @Operation(summary = "表单配置删除")
    @RequestPermissions("flowable:form:remove")
    @Logs(title = "表单配置删除", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public Result remove(String formIds) {
        List<Long> ids = ToolUtils.setIdsToList(formIds);
        return toResult(formService.removeIds(ids));
    }

    @Operation(summary = "导出流程表单列表")
    @RequestPermissions("flowable:form:export")
    @Logs(title = "流程表单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(WfForm wfForm) {
        formService.export(wfForm);
    }

}
